﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Furion.Application
{


    /// <summary>
    /// 字典 DictionaryItem
    /// </summary>
    [AppAuthorize, ApiDescriptionSettings("System", Name = "DictionaryItem", Order = 1, Tag = "字典DictionaryItem")]
    public class DictionaryItemService : IDynamicApiController
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IRepository<Furion.Core.DictionaryItem> _dictionaryItemRepository;
        private readonly IAuthorizationManager _authorizationManager;



        public DictionaryItemService(IHttpContextAccessor httpContextAccessor,
            IRepository<Furion.Core.DictionaryItem> dictionaryItemRepository,
            IAuthorizationManager authorizationManager)
        {
            _httpContextAccessor = httpContextAccessor;
            _dictionaryItemRepository = dictionaryItemRepository;
            _authorizationManager = authorizationManager;
        }


        /// <summary>
        /// 新增 DictionaryItem
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_Add), HttpPost("/DictionaryItem/Add"), ApiDescriptionSettings(Name = "Add")]
        public async Task<int> AddAsync([Required] DictionaryItemAddInput input)
        {
            var addInput = input.Adapt<DictionaryItem>();
            var entryEntity = await _dictionaryItemRepository.InsertNowAsync(addInput);
            return entryEntity.Entity.Id;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_Edit), HttpPost("/DictionaryItem/Edit"), ApiDescriptionSettings(Name = "Edit")]
        public async Task EditAsync([Required] DictionaryItemEditInput input)
        {
            var editInput = input.Adapt<DictionaryItem>();
            await _dictionaryItemRepository.UpdateAsync(editInput);
        }


        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_List), HttpGet("/DictionaryItem/List"), ApiDescriptionSettings(Name = "List")]
        public async Task<List<DictionaryItem>> ListAsync([FromQuery] TreePageInput input)
        {
            var list = await _dictionaryItemRepository
                .Where(a => a.IsDeleted == input.IsDeleted)
                .Where(input.DictionaryId > -1, a => a.DictionaryId == input.DictionaryId)                
                .OrderBy(a => a.Ord)
                .ToListAsync();

            return list.Adapt<List<DictionaryItem>>();
        }


        /// <summary>
        /// Tree
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_List), HttpGet("/DictionaryItem/Tree"), ApiDescriptionSettings(Name = "Tree")]
        public async Task<List<DictionaryItemTree>> TreeAsync([FromQuery] TreePageInput input)
        {
            var list = await _dictionaryItemRepository
                .Where(a => a.IsDeleted == input.IsDeleted)
                .Where(input.DictionaryId > -1, a => a.DictionaryId == input.DictionaryId)
                .OrderBy(a => a.Ord)
                .ToListAsync();

            var trees = list.Where(a => a.ParentId == 0).Adapt<List<DictionaryItemTree>>();

            foreach (var item in trees)
            {
                item.children = GetChilds(list, item.Id);
            }
            return trees;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DataSource"></param>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        [ApiDescriptionSettings(false)]
        public List<DictionaryItemTree> GetChilds(List<DictionaryItem> DataSource, int ParentId)
        {
            var items = DataSource.Where(a => a.ParentId == ParentId).OrderBy(a=>a.Ord).ToList().Adapt<List<DictionaryItemTree>>();
            foreach (var item in items)
            {
                item.children = GetChilds(DataSource, item.Id);
            }
            return items;
        }



        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_List), HttpGet("/DictionaryItem/PageList"), ApiDescriptionSettings(Name = "PageList")]
        public async Task<PagedList<DictionaryItem>> PageListAsync([FromQuery] DictionaryItemPageInput input)
        {
            var hasKeyword = !string.IsNullOrEmpty(input.Keyword?.Trim());
            var list = await _dictionaryItemRepository
                .Where(a=>a.IsDeleted==input.IsDeleted)
                .Where(input.DictionaryId>-1,a=>a.DictionaryId == input.DictionaryId)
                .Where(hasKeyword, a => a.Name.Contains(input.Keyword.Trim()))
                .OrderByDescending(a => a.Id)
                .ToPagedListAsync(input.PageIndex, input.PageSize);

            return list.Adapt<PagedList<DictionaryItem>>();
        }




        /// <summary>
        /// 详细信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_List), HttpGet("/DictionaryItem/Detail"), ApiDescriptionSettings(Name = "Detail")]
        public async Task<DictionaryItem> DetailAsync([FromQuery, Required] long Id)
        {
            var info = await _dictionaryItemRepository.Where(a => a.Id.Equals(Id)).FirstOrDefaultAsync();

            return info;

        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_Delete), HttpPost("/DictionaryItem/Delete"), ApiDescriptionSettings(Name = "Delete")]
        public async Task DeleteRange(DictionaryItemDeleteRangeInput input)
        {
            //await _dictionaryItemRepository.Context.BatchUpdate<DictionaryItem>()
            //   .Set(b => b.IsDeleted, b => true)
            //   .Where(b => input.Ids.Contains(b.Id))
            //   .ExecuteAsync();

            await _dictionaryItemRepository.Entities.Where(b => input.Ids.Contains(b.Id)).ExecuteUpdateAsync(s => s.SetProperty(b => b.IsDeleted, b => true));

        }

        /// <summary>
        /// 删除后恢复
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_Recovery), HttpPost("/DictionaryItem/Recovery"), ApiDescriptionSettings(Name = "Recovery")]
        public async Task RecoveryRange(DictionaryItemDeleteRangeInput input)
        {
            await _dictionaryItemRepository.Entities.Where(b => input.Ids.Contains(b.Id)).ExecuteUpdateAsync(s => s.SetProperty(b => b.IsDeleted, b => false));
        }



    }
}
